﻿Public Class DocTemplateExplorer
    Inherits ExplorerTreeList

    Private EditPanePL As PLBase

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
        'AddHandler MainContextMenuBeforePopup, AddressOf Spec_ContextMenuBeforePopup
    End Sub

    Public Sub New()
        MyBase.New()
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
        'AddHandler MainContextMenuBeforePopup, AddressOf Spec_ContextMenuBeforePopup
    End Sub

    'Private Sub DocTemplateExplorer_Load(sender As Object, e As EventArgs) Handles Me.Load
    '    TreeActionOptionListLoad()
    '    MetaControlActionOptionList.ApplySecurity()
    'End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        ' Memory management / Help the GC
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        Dim guidEnfant As Guid
        Dim aPLName As String
        Dim aMetaQueryName As String
        Dim aMetaQuery As MetaQuery

        Dim frmCaption As String = "Document Template"

        Try
            guidEnfant = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)

            aPLName = DirectCast(NodeDataRow("PL"), String)
            aMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
            aMetaQuery = MetaQuery.GetMetaQueryByName(aMetaQueryName)

            EditPanePL = PLBase.CreateInstanceOf(aPLName, aMetaQueryName)


            If AppManager.ApplicationOption.DescriptionColumnIndex = 3 Then
                frmCaption = NodeDataRow("Description_ALT").ToString
            Else
                frmCaption = NodeDataRow("Description_PRM").ToString
            End If


            Select Case FocusedNodeLevel
                Case 0
                    'Document List
                Case 1
                    'Document by Class
                    Dim classId_FK = IIf(IsDBNull(NodeDataRow("ClassId_FK")), Guid.Empty, DirectCast(NodeDataRow("ClassId_FK"), Guid))
                    EditPanePL.SQLWhere = String.Format("ClassId_FK = '{0}'", classId_FK)
                Case 2
                    'Document Template
                    If Not Guid.Empty.Equals(guidEnfant) Then
                        EditPanePL.SQLWhere = String.Format("{0} = '{1}'", aMetaQuery.GUIDField, guidEnfant)
                    End If
                Case 3
                    'Document Template Revision
            End Select


            aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = EditPanePL, .SecureGrid = True, .Name = aMetaQuery.Caption, .FormCtrlCD = MyBase.PanelName}
            AddHandler aGridControl.InitGridActionOptionList, AddressOf GridActionOptionListLoad
            AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf DocumentTemplate_ContextMenuBeforePopup

        Catch ex As Exception
            MessageWindow.ShowMessage(ex)

        End Try

        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub


   
End Class